/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    let length = prices.length;
    if(length<=1)return 0;

    let min = prices[0],
        res = 0,
        i = 1;

    for(;i<length;i++){
        min = prices[i-1]<min?prices[i-1]:min;
        let tmp = prices[i] - min;
        res = tmp>res?tmp:res;
    }

    return res;
};